#!/usr/bin/python -S
"""
records_test.py: Tests for records.py

Longer description here.
"""

__author__ = 'Andy Chu'


import os
import sys
import unittest

if __name__ == '__main__':
  sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), '../..'))

import records  # module under test


class FooTest(unittest.TestCase):
  def setUp(self):
    pass

  def tearDown(self):
    pass

  def testFoo(self):
    r = records.Record(foo='bar')
    self.assertEqual(r.foo, 'bar')
    self.assertEqual(r["foo"], 'bar')

    r['complex-attribute!!'] = 'value'
    self.assertEqual(r["complex-attribute!!"], 'value')

    # TODO: test edge cases, like __ values

    r.foo = 'newfoo'
    self.assertEqual(r["foo"], 'newfoo')

    return
    # TODO: This is totally broken -- can I set keys or not?
    r.keys = 'newkeys'
    self.assertEqual(r.keys, 'newkeys')
    print r.keys

  def testStr(self):
    # TODO: More extensive tests
    r = records.Record(foo='bar')
    self.assertEqual(str(r), '{"foo": "bar"}')

  def testPrefixSlice(self):
    r = records.Record({
      "undeclared.actor_names": "['input-stream', 'grep', 'fmt']", 
      "undeclared.fmt.format_str": "dssd", 
      "undeclared.fmt.strict": "true", 
      "undeclared.grep.ignore_case": "false", 
      "undeclared.grep.out_field": "", 
      "undeclared.grep.regex": "^def", 
      "undeclared.input_stream_file": "", 
      "undeclared.input_stream_text": "foo bar", 
      "undeclared.input_stream_url": ""
      })
    one = r.prefix_slice('undeclared')
    self.assertEqual(
        one.prefix_slice('grep'),
        records.Record(ignore_case="false", out_field="", regex="^def"))

  def testRemoveKey(self):
    old = records.Record(foo='bar', spam='eggs')
    new = old.remove_key('foo')
    self.assertEqual(new, records.Record(spam='eggs'))
    self.assert_(old is new)

  def testInsertItem(self):
    old = records.Record(foo='bar', spam='eggs')
    new = old.insert_item('foo', 'newbar')
    self.assertEqual(new, records.Record(foo='newbar', spam='eggs'))
    self.assert_(old is new)


if __name__ == '__main__':
  unittest.main()
